﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Proposal.Models;

namespace Sautom.Modules.Proposal.ViewModels
{
    public sealed class CountriesListViewModel : NavigationAwareNotificationObject, IRegionMemberLifetime
    {
        #region Constructor
        public CountriesListViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            EditCountryCommand = new DelegateCommand<string>(ExecuteEditCountryCommand, (isCreateNew) => isCreateNew == "true" || SelectedItem != null);

            QueriesServiceClient = serviceFactory.GetQueriesService();
        }
        #endregion

        #region Tech properties

        public IQueriesService QueriesServiceClient { get; set; }
        #endregion

        #region Commands
        public DelegateCommand<string> EditCountryCommand { get; private set; }

        private void ExecuteEditCountryCommand(string isCreateNew)
        {
            var uriQuery = new UriQuery();
            if (isCreateNew != "true" && SelectedItem != null)
            {
                uriQuery.Add("countryId", SelectedItem.Id.ToString());
            }

            var uri = new Uri(PathProvider.CountryEdit + uriQuery, UriKind.Relative);

            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }

        #endregion

        #region For View properties

        private IRegionManager RegionManager { get; set; }

        private CountryModel _selectedItem;
        public CountryModel SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
                EditCountryCommand.RaiseCanExecuteChanged();
            }
        }

        private ObservableCollection<CountryModel> _countries;
        public ObservableCollection<CountryModel> Countries
        {
            get { return _countries; }
            set
            {
                _countries = value;
                RaisePropertyChanged(() => Countries);
            }
        }

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            Mapper.CreateMap<CountryWitCitiesDtoOutput, CountryModel>();

            var uiDispather = Dispatcher.CurrentDispatcher;
            (new Thread(() =>
                            {
                                List<CountryWitCitiesDtoOutput> countries = QueriesServiceClient.GetAllCountriesWithCities().ToList();

                                uiDispather.BeginInvoke(DispatcherPriority.Normal,
                                                        (Action)(() => Countries = Mapper.Map(countries, Countries)));
                            })).Start();
        }

        #endregion

        #region Implementation of IRegionMemberLifetime

        public bool KeepAlive
        {
            get { return true; }
        }

        #endregion
    }
}